<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src="<c:url value='/js/plug/area/load_area.js'/>"></script>


<div id="cc_providers" class="easyui-layout" style="width:100%;height:100%;" fit="true">
    <div data-options="region:'center',split:true" style="width: 100%;height: 100%">
		<table id="belongListGrid"></table>
	</div>
</div>  
<script>

var toolbar = [{
	text:'添加',
    iconCls:'ext-icon-note_add',
    handler:function(){
    	chooseBuyer();
    }
},{
	text:'删除',
    iconCls:'ext-icon-note_delete',
    handler:function(){
    	var rows = providerBelongBuyersTable.getCheckedData();
		if(rows==null || rows.length==0){
			alertWarning("请选中要删除的数据!");
			return;
		}
		if(confirm("确定批量删除?")){
			var ids = [];
			for(var i=0; i<rows.length; i++){
				ids.push(rows[i].belongId);
			}
			var belongIdList = ids.toString();
			var param = {"belongIdList":belongIdList};
			$.pmsAjax({
				url:'<c:url value="/emcproviderbelongbuyer/deleteMultipleEmCProviderBelongBuyers.do"/>',
				data:param,
				success:function(data){
					if(data.code=="200"){
						alertShow("批量删除成功！");
						providerBelongBuyersTable.refresh();
					}else{
						alertError(data.result);
					}
				}
			});
		}
    }
}];
var providerBelongBuyersTable;
$(function(){
	initProviderBelongBuyers();
});

function initProviderBelongBuyers(){
	providerBelongBuyersTable = $('#belongListGrid').pDataGrid({
		toolbar:toolbar,
		columns:[[
				   {field:'ck',checkbox:true},
	               {field:'buyerName',title:'客户名称',align:'center'},
	               {field:'buyerPhone',title:'联系电话',align:'center'},
	               {field:'buyerAreaName',title:'所属区域',align:'center'},
	               {field:'buyerAddress',title:'地址',align:'center'},
	               {field:'isValid',title:'是否审核',align:'center',formatter:function(value){
						if(value=="0"){
							return '未审';
						}else{
							return '审核';
						}
		           }},
		           {field:'action',title : '操作',align : 'center',
						formatter : function(value, row, index) {
							var str = '';
								str += ' <img class="iconImg ext-icon-note_delete" title="删除" onclick="deleteProviderBelongBuyer('+row.belongId+',' + index + ');"/>';
								return str;
						}
					} 
		          ]],
		    url:'<c:url value="/emcproviderbelongbuyer/searchPageProviderBelongBuyer.do" />',  
		    pagination:true,//分页控件
		    pageSize:30,
		    singleSelect:false,
		    rownumbers:true,
		    queryParams:{
				"providerCode":"<%=request.getParameter("providerCode")%>"
		    },
		    onLoadSuccess : function(data) {
				var panel = $(this).datagrid('getPanel');
				var tr = panel.find('div.datagrid-body tr');
				tr.each(function() {
					var td = $(this).children('td[field="buyerName"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="buyerAreaName"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="buyerAddress"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
			}
	}); 
}

function chooseBuyer(){
	var params = {"providerCode":"<%=request.getParameter("providerCode")%>"};
	$.pmsDialog({
		id:"w_chooseBuyer",
		title:'选择客户',
		method:'post',
		isIf:'true',
		params:params,
		href:'<c:url value="/views/jsp/customer/buyer/buyer_list_forchoose_s.jsp"/>',
		width:780,
	});
}
//选择供应商
function getBuyer(buyerCodeList){
	$("#w_chooseBuyer").dialog('close');
	var param = {"providerCode":"<%=request.getParameter("providerCode")%>",
			     "buyerCodeList":buyerCodeList};
	$.pmsAjax({
		url:'<c:url value="/emcproviderbelongbuyer/insertProviderBelongBuyer.do"/>',
		data:param,
		success:function(data){
			if(data.code=="200"){
				alertShow("添加成功");
				providerBelongBuyersTable.refresh();
			}else{
				alertError(data.result);
			}
		}
	});
}

function deleteProviderBelongBuyer(code,i){
	if(confirm("确定删除?")){
		var param = {"belongId":code};
		$.pmsAjax({
			url:'<c:url value="/emcproviderbelongbuyer/deleteProviderBelongBuyer.do"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					alertShow("删除成功");
					providerBelongBuyersTable.refresh();
				}else{
					alertError(data.result);
				}
			}
		});
	}
}

$("#s_loc").LocationSelect({
	data:"<c:url value='/js/plug/area/areas.json' />"
});
	
</script>
